package com.cloudxingkong.dianyinke.NewTeacher.Ui;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.NewTeacher.Utils.SysExitUtil;
import com.cloudxingkong.dianyinke.NewTeacher.bean.QryTeacher;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import de.hdodenhof.circleimageview.CircleImageView;

public class MineCenterActivity extends AppCompatActivity implements View.OnClickListener {

    private SharedPreferences sharedPreferences;
    private TextView tvName;
    private TextView tvYiDaoZhang;
    private TextView tvWeiDaoZhang;
    private TextView tvAliasname;
    private TextView tvInst;
    private TextView tvSeniority;
    private String sessionID;
    private String avatarPath;
    private CircleImageView ivAvatar;
    private LinearLayout llAliasname;
    private LinearLayout llMusic;
    private LinearLayout llSeniorty;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_self);
        SysExitUtil.activityList.add(this);
        sharedPreferences = getSharedPreferences("data_dyk",0);
        init();
        loadData();
        clickToUR();
    }

    private void clickToUR() {
        ivAvatar.setOnClickListener(this);
        llAliasname.setOnClickListener(this);
        llMusic.setOnClickListener(this);
        llSeniorty.setOnClickListener(this);
    }

    private void loadData() {
        tvName.setText(sharedPreferences.getString("qryTname", ""));
        tvYiDaoZhang.setText(sharedPreferences.getString("qryTpayed", ""));
        tvWeiDaoZhang.setText(sharedPreferences.getString("qryTnopay",""));
        tvAliasname.setText(sharedPreferences.getString("qryTAliasname",""));
        tvSeniority.setText(sharedPreferences.getString("qryTseniorty","")+" 年");
        tvInst.setText(sharedPreferences.getString("qryTinst", ""));
        String qryTavatar = sharedPreferences.getString("qryTavatar", "");

        if (!qryTavatar.equals("")) {
            ImageLoader.getInstance().displayImage(qryTavatar,ivAvatar);
        }
        qryTeacher();
    }
    //请求老师头像
    private void qryTeacher() {
        RequestParams params = new RequestParams(ThisUri.SERVER_YUMING + ThisUri.THIS_RESUME);
        params.addHeader("cookie", sessionID);

        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                QryTeacher objBean = gson.fromJson(result, QryTeacher.class);
                if (objBean.isSuccess()) {
                    QryTeacher.ObjBean obj = objBean.getObj();
                    String avatar = obj.getAvatar();
                    ImageLoader.getInstance().displayImage(avatar, ivAvatar);

                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putString("qryTavatar", obj.getAvatar() + "");
                    edit.putString("qryTAliasname", obj.getAliasname());
                    edit.putString("qryTname", obj.getMemname());
                    edit.commit();

                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                }else{
                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                Log.e("test","获取老师资料失败");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    private void init() {
        tvName = (TextView) findViewById(R.id.ms_myname);
        tvYiDaoZhang = (TextView) findViewById(R.id.ms_yidaozhang);
        tvWeiDaoZhang = (TextView) findViewById(R.id.ms_weidaozhang);
        tvAliasname = (TextView) findViewById(R.id.ms_alisname);
        tvInst = (TextView) findViewById(R.id.ms_inst);
        tvSeniority = (TextView) findViewById(R.id.ms_sniority);
        ivAvatar = (CircleImageView) findViewById(R.id.ms_touxiang);
        llAliasname = (LinearLayout) findViewById(R.id.ms_nicheng);
        llMusic = (LinearLayout) findViewById(R.id.ms_musicinstrument);
        llSeniorty = (LinearLayout) findViewById(R.id.ms_teachyear);
        sessionID = CookieHelper.getSessionID(sharedPreferences);
        progressDialog=new ProgressDialog(this);
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(false);
        progressDialog.setTitle(null);
        progressDialog.setMessage("加载中...");
    }

    public void backActivity(View view) {
        finish();
    }

    public void exitThisApp(View view) {
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("提示！").setMessage("确定退出当前登录吗？")
                .setNegativeButton("取消", null)
                .setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    private ProgressDialog progressDialog;

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        progressDialog = ProgressDialog.show(MineCenterActivity.this, null, "正在操作...", true, false);
                        RequestParams params = new RequestParams(ThisUri.SERVER_YUMING + ThisUri.SIGN_OUT);
                        params.addHeader("cookie", sessionID);
                        x.http().post(params, new Callback.CommonCallback<String>() {
                            @Override
                            public void onSuccess(String result) {
                                progressDialog.dismiss();
                                Intent intent = new Intent(MineCenterActivity.this, Login.class);
                                intent.putExtra("msg", "empty");
                                startActivity(intent);
                                Log.e("test", "有多少activity " + SysExitUtil.activityList.size());
                                SysExitUtil.exit();
                            }

                            @Override
                            public void onError(Throwable ex, boolean isOnCallback) {
                                progressDialog.dismiss();
                                Intent intent = new Intent(MineCenterActivity.this, Login.class);
                                intent.putExtra("msg", "empty");
                                startActivity(intent);
                                Log.e("test", "有多少activity " + SysExitUtil.activityList.size());
                                SysExitUtil.exit();
                            }

                            @Override
                            public void onCancelled(CancelledException cex) {

                            }

                            @Override
                            public void onFinished() {

                            }
                        });

                    }
                }).create().show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ms_touxiang:
            case R.id.ms_nicheng:
            case R.id.ms_musicinstrument:
            case R.id.ms_teachyear:
                Intent intent = new Intent(MineCenterActivity.this, UploadResume.class);
                startActivityForResult(intent,161);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 161) {
            if (resultCode == 152) {
                progressDialog .show();
                loadData();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
